﻿using System;
using System.Collections.Generic;
using System.Text;
using FieldDevice.ModBus;
using FieldLib;
using System.ComponentModel;

namespace FieldDevice.Devices
{
    class DeviceACR330ELH:Device
    {
      
        public DeviceACR330ELH(byte address)
            : base(DeviceCategory.PowerMeter, address)
        {
            Initialize();
        }

        byte[] RegistersValue = new byte[522];

        float Current_Ratio = 640;
        float P_value = 0.00f;
        float Q_value = 0.00f;
        float S_value = 0.00f;
        float EPI = 0.00f;
        float EQL = 0.00f;

        public override void DebugReads()
        {
            if (DeviceManager.GlobalDeviceManager.ModBusIO.IsEmulation)
                this.UpdateReads();
            else
                base.DebugReads();
        }
        public void UpdateReads()

        {
            base.ReadAndUpdateValue(Emulation.ACR330ELHEmulator.ADDR_DI1,1);
            base.ReadAndUpdateValue(Emulation.ACR330ELHEmulator.ADDR_DI1, 37);
        }

        void Initialize()
        {
            base.RequestRegister(Emulation.ACR330ELHEmulator.ADDR_DI1, 1);
            base.RequestRegister(Emulation.ACR330ELHEmulator.ADDR_Va, 38);
            base.RequestRegister(Emulation.ACR330ELHEmulator.ADDR_EPI_Hi, 6);
        }

        /// <summary>
        /// 
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="begin"></param>
        /// <param name="registeraddress"></param>
        /// <returns></returns>
        protected override float Translate(byte[] value, int begin, ushort registeraddress)
        {   
            int tmpValue = value[begin];
            tmpValue <<= 8;
            tmpValue += value[begin + 1];
            switch (registeraddress)
            { 
                case Emulation.ACR330ELHEmulator.ADDR_DI1:
                    base.RecordBooleanRead(OnOffValueName, (tmpValue & 0x0001) == 1);
                    break;
                //case Emulation.ACR330ELHEmulator.ADDR_DO1:
                //     RecordRead("DO1", tmpValue);
                //     break;
                //case Emulation.ACR330ELHEmulator.ADDR_DO2:
                //     RecordRead("DO2", tmpValue);
                //     break;
                //case Emulation.ACR330ELHEmulator.ADDR_DO3:
                //     RecordRead("DO3", tmpValue);
                //     break;
  
                case Emulation.ACR330ELHEmulator.ADDR_Va:
                    RecordRead("VA", tmpValue / 10.0f);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_Vb:
                    RecordRead("VB", tmpValue / 10.0f);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_Vc:
                    RecordRead("VC", tmpValue / 10.0f);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_Vab:
                    RecordRead("Vab", tmpValue / 10.0f);
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_Vbc:
                    RecordRead("Vbc", tmpValue / 10.0f);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_Vca:
                    RecordRead("Vca", tmpValue / 10.0f);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_Ia:
                    RecordRead("IA", (tmpValue / 1000.0f) * Current_Ratio);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_Ib:
                    RecordRead("IB", (tmpValue / 1000.0f) * Current_Ratio);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_Ic:
                    RecordRead("IC", (tmpValue / 1000.0f) * Current_Ratio);
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_F:
                    RecordRead("FR", tmpValue / 100.0f);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_Factor:
                    RecordRead("PF", tmpValue / 1000.0f);
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_P_Hi:
                    P_value = tmpValue*65536;
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_P_Low:
                    P_value += tmpValue;
                    RecordRead("P", (P_value*Current_Ratio) / 100.0f);
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_Q_Hi:
                    Q_value = tmpValue * 65536;
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_Q_Low:
                    Q_value += tmpValue;
                    RecordRead("Q", (Q_value*Current_Ratio)/ 100.0f);
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_S_Hi:
                    S_value = tmpValue * 65536;
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_S_Low:
                    S_value += tmpValue;
                    RecordRead("S", (S_value*Current_Ratio) / 100.0f);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_EPI_Hi:
                    EPI = tmpValue * 65536;
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_EPI_Low:
                    EPI += tmpValue;
                    RecordRead("EPI", (EPI * Current_Ratio) / 100.0f);
                    break;

                case Emulation.ACR330ELHEmulator.ADDR_EQL_Hi:
                    uint ui = FieldLib.Utility.BytesToUInt_SmallEndian(value, begin);
                    EQL = tmpValue * 65536;
                    break;
                case Emulation.ACR330ELHEmulator.ADDR_EQL_Low:
                    EQL += tmpValue;
                    RecordRead("EQL", (EQL * Current_Ratio) / 100.0f);
                    break;
                default:
                    break;

            }
            return 0.0f;
        }


        public class ACR1330ELHConfig : DeviceConfigBase
        {
            public ACR1330ELHConfig()
            {
                this.PowerRatio = 1.0f;
            }

            [Category("电表参数"), Description("ACR330表面板读数与Modbus存在倍率关系。此参数设置此倍率关系")]
            public float PowerRatio { get; set; }
        }
    
    }
}
